package site.zhouinfo.function;

/**
 * 使用高阶函数之前的求和、求平方和、求立方和
 *
 * @author zhou
 * @create 2017-02-08 10:06
 * @email zhouinfo@qq.com
 */
public class CommonFunction {
    public static int identity(int x) {
        return x;
    }

    public static int sum_identity(int a, int b) {
        int sum = 0;
        for (int i = a; i <= b; i++) {
            sum += identity(i);
        }
        return sum;
    }

    public static int square(int x) {
        return x * x;
    }

    public static int sum_square(int a, int b) {
        int sum = 0;
        for (int i = a; i <= b; i++) {
            sum += square(i);
        }
        return sum;
    }

    public static int cube(int x) {
        return x * x * x;
    }

    public static int sum_cube(int a, int b) {
        int sum = 0;
        for (int i = a; i <= b; i++) {
            sum += cube(i);
        }
        return sum;
    }

    public static void main(String[] args) {
        int a = 1, b = 10;
        System.out.println(sum_identity(a, b));
        System.out.println(sum_square(a, b));
        System.out.println(sum_cube(a, b));
    }
}
